package com.irdstudio.bsp.executor.core.dao;

import com.irdstudio.bsp.executor.core.dao.domain.MigrateTableInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 迁移表信息类-[表名: 迁移表信息(migrate_table_info)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-13
 */
public class MigrateTableInfoDao {

	/* 连接对象 */
	Connection conn = null;	

	public MigrateTableInfoDao(Connection conn){
		this.conn = conn;
	}


    /**
	* 新增一行
	* @return int
	*/
    public int insertMigrateTableInfo(MigrateTableInfo migrateTableInfo) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("insert into migrate_table_info ( "
                + "table_id,subs_code,table_code,table_name,table_record_total,table_migrate_tag,source_subs_code,table_use_tag,table_use_desc,migrate_flag,migrate_reason"
                + ") values ("+ "?,?,?,?,?,?,?,?,?,?,?" +")"
                );
            ps.setObject(1, migrateTableInfo.getTableId());
            ps.setObject(2, migrateTableInfo.getSubsCode());
            ps.setObject(3, migrateTableInfo.getTableCode());
            ps.setObject(4, migrateTableInfo.getTableName());
            ps.setObject(5, migrateTableInfo.getTableRecordTotal());
            ps.setObject(6, migrateTableInfo.getTableMigrateTag());
            ps.setObject(7, migrateTableInfo.getSourceSubsCode());
            ps.setObject(8, migrateTableInfo.getTableUseTag());
            ps.setObject(9, migrateTableInfo.getTableUseDesc());
            ps.setObject(10, migrateTableInfo.getMigrateFlag());
            ps.setObject(11, migrateTableInfo.getMigrateReason());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("insert MigrateTableInfo is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键删除一行记录
	* @return int
	*/
    public int deleteByPk(MigrateTableInfo migrateTableInfo) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("delete from migrate_table_info where 1=1"
                + " AND table_id = ? "
                );
            ps.setObject(1, migrateTableInfo.getTableId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("delete MigrateTableInfo is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
	}

    /**
	* 根据主键更新一行记录
	* @return int
	*/
    public int updateByPk(MigrateTableInfo migrateTableInfo) throws SQLException {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement("update migrate_table_info set "
                + " table_id = ? ,"
                + " subs_code = ? ,"
                + " table_code = ? ,"
                + " table_name = ? ,"
                + " table_record_total = ? ,"
                + " table_migrate_tag = ? ,"
                + " source_subs_code = ? ,"
                + " table_use_tag = ? ,"
                + " table_use_desc = ? ,"
                + " migrate_flag = ? ,"
                + " migrate_reason = ? "
                + " where 1=1"
                + " AND table_id = ? "
                );
            ps.setObject(1, migrateTableInfo.getTableId());
            ps.setObject(2, migrateTableInfo.getSubsCode());
            ps.setObject(3, migrateTableInfo.getTableCode());
            ps.setObject(4, migrateTableInfo.getTableName());
            ps.setObject(5, migrateTableInfo.getTableRecordTotal());
            ps.setObject(6, migrateTableInfo.getTableMigrateTag());
            ps.setObject(7, migrateTableInfo.getSourceSubsCode());
            ps.setObject(8, migrateTableInfo.getTableUseTag());
            ps.setObject(9, migrateTableInfo.getTableUseDesc());
            ps.setObject(10, migrateTableInfo.getMigrateFlag());
            ps.setObject(11, migrateTableInfo.getMigrateReason());
            ps.setObject(12, migrateTableInfo.getTableId());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new SQLException("update MigrateTableInfo is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }

	}

    /**
	* 根据主键查询一行记录
	* @return int
	*/
    public MigrateTableInfo queryByPk(MigrateTableInfo migrateTableInfo) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        MigrateTableInfo result = null;
        try {
            ps = conn.prepareStatement("select table_id,subs_code,table_code,table_name,table_record_total,table_migrate_tag,source_subs_code,table_use_tag,table_use_desc,migrate_flag,migrate_reason,subs_ds_code from migrate_table_info where 1=1 "
                + " AND table_id = ? "
                );
            ps.setObject(1, migrateTableInfo.getTableId());
            rs = ps.executeQuery();
            if (rs.next()) {
				result = new MigrateTableInfo();
				result.setTableId(rs.getString("table_id"));
				result.setSubsCode(rs.getString("subs_code"));
				result.setTableCode(rs.getString("table_code"));
				result.setTableName(rs.getString("table_name"));
				result.setTableMigrateTag(rs.getString("table_migrate_tag"));
				result.setSourceSubsCode(rs.getString("source_subs_code"));
				result.setTableUseTag(rs.getString("table_use_tag"));
				result.setTableUseDesc(rs.getString("table_use_desc"));
				result.setMigrateFlag(rs.getString("migrate_flag"));
				result.setMigrateReason(rs.getString("migrate_reason"));
				result.setSubsDsCode(rs.getString("subs_ds_code"));
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateTableInfo is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	* 根据 migrateTableInfo 参数列表查询 不分页
	* @return int
	*/
    public List<MigrateTableInfo> queryAll(MigrateTableInfo migrateTableInfo) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<MigrateTableInfo> result = new ArrayList<>();
        try {
            String sql = "select table_id,subs_code,table_code,table_name,table_record_total,table_migrate_tag,source_subs_code,table_use_tag,table_use_desc,migrate_flag,migrate_reasonfrom migrate_table_info where 1=1 ";
                if (migrateTableInfo.getTableId() != null) {
                    sql = sql + " AND table_id =  '" + migrateTableInfo.getTableId() + "'";
                }
                if (migrateTableInfo.getSubsCode() != null) {
                    sql = sql + " AND subs_code =  '" + migrateTableInfo.getSubsCode() + "'";
                }
                if (migrateTableInfo.getTableCode() != null) {
                    sql = sql + " AND table_code =  '" + migrateTableInfo.getTableCode() + "'";
                }
                if (migrateTableInfo.getTableName() != null) {
                    sql = sql + " AND table_name =  '" + migrateTableInfo.getTableName() + "'";
                }
                if (migrateTableInfo.getTableRecordTotal() != null) {
                    sql = sql + " AND table_record_total =  '" + migrateTableInfo.getTableRecordTotal() + "'";
                }
                if (migrateTableInfo.getTableMigrateTag() != null) {
                    sql = sql + " AND table_migrate_tag =  '" + migrateTableInfo.getTableMigrateTag() + "'";
                }
                if (migrateTableInfo.getSourceSubsCode() != null) {
                    sql = sql + " AND source_subs_code =  '" + migrateTableInfo.getSourceSubsCode() + "'";
                }
                if (migrateTableInfo.getTableUseTag() != null) {
                    sql = sql + " AND table_use_tag =  '" + migrateTableInfo.getTableUseTag() + "'";
                }
                if (migrateTableInfo.getTableUseDesc() != null) {
                    sql = sql + " AND table_use_desc =  '" + migrateTableInfo.getTableUseDesc() + "'";
                }
                if (migrateTableInfo.getMigrateFlag() != null) {
                    sql = sql + " AND migrate_flag =  '" + migrateTableInfo.getMigrateFlag() + "'";
                }
                if (migrateTableInfo.getMigrateReason() != null) {
                    sql = sql + " AND migrate_reason =  '" + migrateTableInfo.getMigrateReason() + "'";
                }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
				MigrateTableInfo obj = new MigrateTableInfo();
				obj.setTableId(rs.getString("table_id"));
				obj.setSubsCode(rs.getString("subs_code"));
				obj.setTableCode(rs.getString("table_code"));
				obj.setTableName(rs.getString("table_name"));
				obj.setTableMigrateTag(rs.getString("table_migrate_tag"));
				obj.setSourceSubsCode(rs.getString("source_subs_code"));
				obj.setTableUseTag(rs.getString("table_use_tag"));
				obj.setTableUseDesc(rs.getString("table_use_desc"));
				obj.setMigrateFlag(rs.getString("migrate_flag"));
				obj.setMigrateReason(rs.getString("migrate_reason"));
                result.add(obj);
			}
        } catch (SQLException e) {
            throw new SQLException("update MigrateTableInfo is Wrong!" + e.getMessage());
        } finally {
            close(rs, null, ps);
        }
        return result;
	}

    /**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
    protected void close(ResultSet theRs, Statement theStmt, PreparedStatement thePs) {
        try {
            if (theRs != null)
                theRs.close();
            if (theStmt != null)
                theStmt.close();
            if (thePs != null)
                thePs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
